﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using ZgOffice.Client.UserControlLibs;
using ZgOffice.Client.DelegateEvent;
using ZgOffice.Client.UserForms;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ZgOffice.Client
{
    public partial class MainFrm : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public MainFrm()
        {
            InitializeComponent();
            bar_loginInfo.Caption = Program.LoginInfo.UserName;
            SysListTree_BindData();
            DiyTableListTree_BindData();
        }
        
        private void SysListTree_BindData()
        {
            treeList_sysMenu.RootValue = "";
            OfficeServiceClient client = new OfficeServiceClient(Program.bind,Program.address);
            this.treeList_sysMenu.DataSource = client.ReadSysListTree().Tables[0];
        }
        private void DiyTableListTree_BindData()
        {
            DiyTableTreeList.RootValue = "";
            OfficeServiceClient client = new OfficeServiceClient(Program.bind, Program.address);
            this.DiyTableTreeList.DataSource = client.ReadDiyTableListTreeView().Tables[0];
        }

        private void treeList_sysMenu_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            //try
            //{
            //    if(e.Node.GetValue(1).ToString()!="")
            //    {
            //        TabListViewFrm tlv = new TabListViewFrm();
            //        tlv.Text = e.Node.GetValue(1).ToString();
            //        tlv.MdiParent = this;
            //        tlv.Show();
            //        tlv.FillDataToView(e.Node.GetValue(1).ToString());
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //    return;
            //}
            try
            {
                if (e.Node.GetValue(1).ToString() != "")
                {
                    OfficeServiceClient client = new OfficeServiceClient(Program.bind, Program.address);
                    MemoryStream stream = new MemoryStream();
                    byte[] buffer;
                    //获取所用块压缩流，并组装
                    while (client.ReadNextBuffer())
                    {
                        buffer = client.GetCurrentBuffer();
                        stream.Write(buffer, 0, buffer.Length);
                    }
                    stream.Position = 0;
                    buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    stream.Close();
                    //解压压缩流
                    byte[] bytes = Compression(buffer, CompressionMode.Decompress);
                    stream = new MemoryStream(bytes);
                    IFormatter formatter = new BinaryFormatter();
                    //反序列化
                    DataSet ds = (DataSet)formatter.Deserialize(stream);
                    stream.Close();
                    TabListViewFrm tlv = new TabListViewFrm();
                    tlv.Text = e.Node.GetValue(1).ToString();
                    tlv.MdiParent = this;
                    tlv.Show();
                    tlv.FillDataToView(ds);
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
        private byte[] Compression(byte[] data, CompressionMode mode)
        {
            DeflateStream zip = null;
            try
            {
                if (mode == CompressionMode.Compress)
                {
                    MemoryStream ms = new MemoryStream();
                    zip = new DeflateStream(ms, mode, true);
                    zip.Write(data, 0, data.Length);
                    zip.Close();
                    return ms.ToArray();
                }
                else
                {
                    MemoryStream ms = new MemoryStream();
                    ms.Write(data, 0, data.Length);
                    ms.Flush();
                    ms.Position = 0;
                    zip = new DeflateStream(ms, mode, true);
                    MemoryStream os = new MemoryStream();
                    int SIZE = 1024;
                    byte[] buf = new byte[SIZE];
                    int l = 0;
                    do
                    {
                        l = zip.Read(buf, 0, SIZE);
                        if (l == 0) l = zip.Read(buf, 0, SIZE);
                        os.Write(buf, 0, l);
                    } while (l != 0);
                    zip.Close();
                    return os.ToArray();
                }
            }
            catch
            {
                if (zip != null) zip.Close();
                return null;
            }
            finally
            {
                if (zip != null) zip.Close();
            }
        }
    }
}